package thread.waitandnotify;

import java.util.concurrent.TimeUnit;

/**
 * @author liweimin
 * @date 2021/4/30 15:41
 * @since
 */
public class Test4 {
    public static final Object lock = new Object();

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (lock) {
                try {
                    System.out.println(Thread.currentThread().getName() + "===========wait begin===========");

                    lock.wait();
                    System.out.println(Thread.currentThread().getName() + "===========wait end===========");

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(() -> {
            synchronized (lock) {
                try {
                    System.out.println(Thread.currentThread().getName() + "===========wait begin===========");

                    lock.wait();
                    System.out.println(Thread.currentThread().getName() + "===========wait end===========");

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(() -> {
            synchronized (lock) {
                try {
                    System.out.println(Thread.currentThread().getName() + "===========wait begin===========");

                    lock.wait();
                    System.out.println(Thread.currentThread().getName() + "===========wait end===========");

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(() -> {
            synchronized (lock) {
                try {
                    System.out.println(Thread.currentThread().getName() + "===========wait begin===========");

                    lock.wait();
                    System.out.println(Thread.currentThread().getName() + "===========wait end===========");

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        synchronized (lock) {
//            lock.notify();//只能唤醒一个线程
            lock.notifyAll();//唤醒所有等待的线程
        }

    }
}
